package com.define.ssh.action.userconfig;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.model.Compdept;
import com.define.ssh.model.UserInfo;
import com.define.ssh.service.CompdeptService;
import com.define.ssh.util.Pager.Order;
import com.define.ssh.util.SystemUtil;
import com.sxit.ecwx.core.utils.Formatu;

/***
 * 
 * @author wwc
 * 
 */
@ParentPackage("userconfig")
public class DeptAction extends BaseAdminAction {

	private Compdept dept;
	private String deptids;

	@Resource(name = "compdeptServiceImpl")
	private CompdeptService compdeptService;

	public String getDeptInfo(){
		return ajax(this.compdeptService.get(dept.getId()));
	}
	
	public String update(){
		Compdept temp=this.compdeptService.get(dept.getId());
		System.out.println("父id为==="+temp.getPid());
		Date updatetime = compdeptService.getUpdateTime("compdept");
		temp.setDeptname(dept.getDeptname());
		temp.setUpdateTime(new Date());
		temp.setUpdatetype(2);//更新类型 1.新增 2.修改 3.删除'
		this.compdeptService.update(temp);
		
		Long modfiytime = updatetime.getTime() / 1000;
		byte[] by = new byte[18];
	        System.arraycopy(Formatu.int2byte(temp.getId()), 0, by,0, 4); /*操作用户 */
			System.arraycopy(Formatu.int2byte(temp.getPid()), 0, by,4, 4); /*操作时间  */
			System.arraycopy(Formatu.int2byte(temp.getCompany().getId()), 0, by,8, 4); /*操作用户 */
			System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,12, 4); /*操作用户 */	
			short opertype=2;
			System.arraycopy(Formatu.short2byte(opertype), 0, by,16, 2); /*操作类型 */
			compdeptService.sendMsg(by, 1004);
		
		return ajax(Status.success);
	}
	public String save(){
		System.out.println("父id为==="+dept.getPid());
		dept.setUpdateTime(new Date());
		dept.setUpdatetype(1);//更新类型 1.新增 2.修改 3.删除'
		Date updatetime = compdeptService.getUpdateTime("compdept");
		int id=this.compdeptService.save(dept);
		Long modfiytime = updatetime.getTime() / 1000;
		UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
		byte[] by = new byte[18];
	        System.arraycopy(Formatu.int2byte(dept.getId()), 0, by,0, 4); /*操作用户 */
			System.arraycopy(Formatu.int2byte(dept.getPid()), 0, by,4, 4); /*操作时间  */
			System.arraycopy(Formatu.int2byte(dept.getCompany().getId()), 0, by,8, 4); /*操作用户 */
			System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,12, 4); /*操作用户 */	
			short opertype=1;
			System.arraycopy(Formatu.short2byte(opertype), 0, by,16, 2); /*操作类型 */
			compdeptService.sendMsg(by, 1004);
		return ajax(this.compdeptService.get(id));
	}
	public String delete(){
		System.out.println("父id为==="+dept.getPid());
		this.compdeptService.delete1(dept.getId());
		this.compdeptService.delete(dept.getId());
		return ajax(Status.success);
	}
	public String getTotalByDeptId(){
		final StringBuffer sb = new StringBuffer();
		int total=this.compdeptService.getTotalByDeptId(dept.getId());
		sb.append("{");
		sb.append("total:" + total);
		sb.append("}");
		return ajax(JSONObject.fromObject(sb.toString()).toString());
	}
	public String updateSort(){
		String[] ids=deptids.split(",");
		int i=1;
		for(String idStr:ids){
			if(org.apache.commons.lang3.StringUtils.isNotEmpty(idStr)){
				int id=Integer.valueOf(idStr);
				Compdept cd=this.compdeptService.get(id);
				cd.setSortmanager(i);
				this.compdeptService.update(cd);
				i++;
			}
		}
		return ajax(Status.success);
	}
	
	
	public String getDeptByPid(){
		final StringBuffer sb = new StringBuffer();
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("pid", dept.getPid());
		pager.setOrder(Order.asc);
		pager.setOrderBy("sortmanager");
		pager.setSearchMap(map);
		pager.setPageSize(100);
		pager=this.compdeptService.findPager(pager);
		List<Compdept> list=(List<Compdept>) pager.getResult();
		sb.append("[");
		for (int i = 0; i < list.size(); i++) {
			final Compdept dept = (Compdept) list.get(i);
			sb.append("{");
			sb.append("id:" + dept.getId());
			sb.append(",pId:" + dept.getPid());
			sb.append(",name:\"" + dept.getDeptname() + "\"");
			sb.append("}");
			if (i != list.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]");
		return ajax(JSONArray.fromObject(sb.toString()).toString());
	}
	
	public Compdept getDept() {
		return dept;
	}

	public void setDept(Compdept dept) {
		this.dept = dept;
	}

	public String getDeptids() {
		return deptids;
	}

	public void setDeptids(String deptids) {
		this.deptids = deptids;
	}
	
}
